package v3.object.command.list;

import galaxy.obj.Point;

import java.util.Map;

import text.TextUtil;
import v3.object.STcfg;
import v3.object.Screen;
import v3.object.Utilities;
import v3.object.command.aCommand;
import v3.object.command.iCommand;
import v3.object.command.servoCommand.OnScreen;
import v3.object.ship.Ship;
import v3.object.space.Sector;
import v3.object.space.SpaceObjType;
import v3.object.space.Universe;
import v3.object.space.iSpaceObj;

public class LongSensor extends aCommand
{
	String zero = "000";

	public LongSensor()
	{
		super();
		commandName = "LONG SENSOR";
	}

	@Override
	public void getHelp(Ship ship)
	{
		String help = "LongSensor [ls]\n\n" + "give the information about the surounding sectors\n" + "info are gouped by the sensibility of the sensor (" + ship.getLongSensorSensibility() + "x"
				+ ship.getLongSensorSensibility() + ")\n" + "" + "For is grup os secotrs show: Stars Enemy Bases\n" 
				+ "Time Cost " + ship.getTimeConsume((this.getClass())) + "\n" 
				+ "Energy Cost " + ship.getEnergyConsume((this.getClass())) + "";
		help = TextUtil.formatHelp(help);
		System.out.println(help);
	}

	@Override
	protected boolean isCommandAccetable(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return true;
	}

	@Override
	protected double calculateEnergy(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getEnergyConsume((this.getClass()));
	}

	@Override
	protected double calculateTime(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getTimeConsume((this.getClass()));
	}

	@Override
	protected void checkInput(String[] input) throws Exception
	{
	}

	@Override
	protected boolean realExecute(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		int group = ship.getLongSensorSensibility();
		String[][] ls = new String[3][3];
		int x1 = ship.getPosition().getiX() - group * 3 - 1;
		int x2 = ship.getPosition().getiX() + group * 3 + 1;
		int y1 = ship.getPosition().getiY() - group * 3 - 1;
		int y2 = ship.getPosition().getiY() + group * 3 + 1;
		int ii = 0;
		int jj = 0;
		for (int x = x1; x < x2; x = x + (group * 2 + 1))
		{
			jj = 0;
			for (int y = y1; y < y2; y = y + (group * 2 + 1))
			{
				int bb = 0;
				int kk = 0;
				int ss = 0;

				for (int xx = 0; xx < group * 2 + 1; xx++)
				{
					for (int yy = 0; yy < group * 2 + 1; yy++)
					{
						Point p = new Point(x + xx, y + yy);
						// System.out.println(p);
						Sector s = universe.getSector(p);
						int info[] = s.getInfo(SpaceObjType.Star,SpaceObjType.Enemy,SpaceObjType.StarBase);
						ss=ss+info[0];
						kk=kk+info[1];
						bb=bb+info[2];
					}
				}
				String sss = (zero + ss);
				sss = sss.substring(sss.length() - 3, sss.length());

				String kkk = (zero + kk);
				kkk = kkk.substring(kkk.length() - 3, kkk.length());

				String bbb = (zero + bb);
				bbb = bbb.substring(bbb.length() - 3, bbb.length());

				ls[jj][ii] = sss + "#" + kkk + "#" + bbb;
				jj++;
			}
			ii++;
		}

		Screen s = new Screen(16, 27);
		//s.fill(".");
		int index = 0;
		for (int x = x1; x < x2; x = x + (group * 2 + 1))
		{
			int row = 4 + index * 4;
			int col = 0;
			s.printAt(row, col, Utilities.right(zero + x, 3) + " |");
			s.printAt(row + 1, col, " -- |");
			s.printAt(row + 2, col, Utilities.right(zero + (x + group * 2), 3) + " |");
			s.printAt(row + 3, col, "-----+");
			index++;
		}
		index = 0;
		for (int y = y1; y < x2; y = y + (group * 2 + 1))
		{
			int row = 0;
			int col = 7 + index * 7;
			s.printAt(row, col, Utilities.right(zero + y, 3) + " |");
			s.printAt(row + 1, col, " -- |");
			s.printAt(row + 2, col, Utilities.right(zero + (y + group * 2), 3) + " |");
			s.printAt(row + 3, col-2, "------+");
			index++;
		}

		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				int row = 4 + i * 4;
				int col = 6 + j * 7;
				String o = ls[i][j];
				// System.out.println(x+" "+y+"\t"+o);
				String[] parts = o.split("#");
				s.printAt(row, col, "S" + parts[0] + " |");
				s.printAt(row + 1, col, "E" + parts[1] + " |");
				s.printAt(row + 2, col, "B" + parts[2] + " |");
				s.printAt(row + 3, col, "-----+");
			}
		}

		iCommand onScreen = new OnScreen(commandName, s.toString());
		onScreen.execute(cfg, universe, ship, commands, input);

		return false;
	}

	@Override
	protected boolean confirmCommand(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return true;
	}

}
